package cn.tqfeiyang.springplus.warehouse.service.impl;

import cn.tqfeiyang.springplus.warehouse.entity.WarehouseEntity;
import cn.tqfeiyang.springplus.warehouse.mapper.WarehouseMapper;
import cn.tqfeiyang.springplus.warehouse.service.WarehouseService;
import cn.tqfeiyang.springplus.warehouse.request.WarehouseDTO;
import cn.tqfeiyang.springplus.warehouse.response.WarehouseVO;
import cn.tqfeiyang.springplus.warehouse.request.WarehouseSTO;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import cn.tqfeiyang.springplus.framework.conversion.ConversionService;
import cn.tqfeiyang.springplus.framework.exception.BusinessException;
import cn.tqfeiyang.springplus.framework.response.ResultCode;
import cn.tqfeiyang.springplus.framework.pagination.PageHelper;
import cn.tqfeiyang.springplus.framework.pagination.PageData;
import jakarta.annotation.Resource;
import org.springframework.util.Assert;

/**
 * 仓库服务实现类
 *
 * @author tqfeiyang
 * @since 2024-08-01
 */
@Service
public class WarehouseServiceImpl extends ServiceImpl<WarehouseMapper, WarehouseEntity> implements WarehouseService {

    /**
     * 对象转换服务
     */
    @Resource
    private ConversionService conversionService;

    /**
     * 仓库映射接口
     */
    @Resource
    private WarehouseMapper warehouseMapper;

    /**
     * 分页查询仓库
     *
     * @param warehouseSTO 查询数据
     * @return 仓库列表
     */
    @Override
    public PageData<WarehouseVO> selectWarehousePage(WarehouseSTO warehouseSTO) {
        Assert.notNull(warehouseSTO, "warehouseSTO不能为空");
        PageHelper<WarehouseEntity> pageHelper = new PageHelper<>(warehouseSTO);
        return pageHelper.selectPage(warehouseMapper, WarehouseVO.class);
    }

    /**
     * 查询仓库
     *
     * @param warehouseId 仓库id
     * @return 仓库信息
     */
    @Override
    public WarehouseVO selectWarehouseInfo(Long warehouseId) {
        Assert.notNull(warehouseId, "warehouseId不能为空");
        WarehouseEntity warehouseEntity = checkWarehouseExists(warehouseId);
        return conversionService.map(warehouseEntity, WarehouseVO.class);
    }

    /**
     * 新建仓库
     *
     * @param warehouseDTO 请求数据
     * @return 仓库id
     */
    @Override
    @Transactional(rollbackFor = Exception.class)
    public Long createWarehouse(WarehouseDTO warehouseDTO) {
        Assert.notNull(warehouseDTO, "warehouseDTO不能为空");
        WarehouseEntity warehouseEntity = conversionService.map(warehouseDTO, WarehouseEntity.class);
        if (warehouseMapper.insert(warehouseEntity) == 0) {
            throw new BusinessException(ResultCode.ENTITY_INSERT_ERROR);
        }
        return warehouseEntity.getWarehouseId();
    }

    /**
     * 修改仓库
     *
     * @param warehouseId 仓库id
     * @param warehouseDTO 请求数据
     */
    @Override
    @Transactional(rollbackFor = Exception.class)
    public void updateWarehouse(Long warehouseId, WarehouseDTO warehouseDTO) {
        Assert.notNull(warehouseId, "warehouseId不能为空");
        Assert.notNull(warehouseDTO, "warehouseDTO不能为空");
        WarehouseEntity originalEntity = checkWarehouseExists(warehouseId);
        WarehouseEntity warehouseEntity = conversionService.map(warehouseDTO, WarehouseEntity.class);
        warehouseEntity.setWarehouseId(warehouseId);
        warehouseEntity.setVersion(originalEntity.getVersion());
        if (warehouseMapper.updateById(warehouseEntity) == 0) {
            throw new BusinessException(ResultCode.ENTITY_UPDATE_ERROR);
        }
    }

    /**
     * 删除仓库
     *
     * @param warehouseId 仓库id
     */
    @Override
    @Transactional(rollbackFor = Exception.class)
    public void deleteWarehouse(Long warehouseId) {
        Assert.notNull(warehouseId, "warehouseId不能为空");
        checkWarehouseExists(warehouseId);
        if (warehouseMapper.deleteById(warehouseId) == 0) {
            throw new BusinessException(ResultCode.ENTITY_DELETE_ERROR);
        }
    }

    /**
     * 检查仓库实体是否存在
     *
     * @param warehouseId 仓库id
     * @return 仓库实体
     */
    protected WarehouseEntity checkWarehouseExists(Long warehouseId) {
        WarehouseEntity warehouseEntity = warehouseMapper.selectById(warehouseId);
        if (warehouseEntity == null) {
            throw new BusinessException(ResultCode.ENTITY_NOT_EXISTS);
        }
        return warehouseEntity;
    }

}
